#!/usr/bin/env bash
###   sh initnode.sh install -c 47.99.240.176 -p 6443 -s terminus-test
## const
INTERFACE=
NODE_NAME=
TOKEN=
SITE=
CLOUD_HOST=
CLOUD_PORT=
KUBERNETES_VERSION="v1.18.8"

## tools
function out_error() {
  echo -e "\033[31m ERROR >> $* \033[0m"
}

function out_success() {
  echo -e "\033[32m $* \033[0m"
}

function out_warn() {
  echo -e "\033[33m WARN >> $* \033[0m"
}

## check
function check_root() {
  if [ $UID -ne 0 ]; then
    out_warn Not root user. Please run "su root" as root.
  fi
}

function check_os() {
  os="$(. /etc/os-release && echo "$ID")"
  osVersion="$(. /etc/os-release && echo "$VERSION_ID")"
  osArch="$(hostnamectl | grep Architecture | awk -F : '{printf $2}')"
  if [[ "$osArch" != " x86-64" ]]; then
    out_warn "OS Architecture $osArch has not support!"
  fi

  case $os in
  centos)
    if [[ $osVersion -lt 7 ]] || [[ $osVersion -gt 8 ]]; then
      out_warn OS $os Version $osVersion has not been tested and may not succeed!
    fi
    ;;
  * | '')
    out_warn OS $os has not been tested and may not succeed!
    ;;
  esac
}

function check_swap() {
  cmdOut="$(swapon -s)"
  if [ -n "$portOut" ]; then
    out_warn Swap started. Please run "swapoff -a" turn off swap.
  fi
}

function check_firewalld() {
  status="$(systemctl status firewalld | grep Active | awk '{print $3}')"
  if [ "${status}" == "(running)" ]; then
    out_warn Firewalld runnning. Please run "systemctl disable firewalld && systemctl stop firewalld" turn off firewalld.
  fi
}

function check_port() {
  for port in 1443 {10249..10259} {51000..51020}; do
    portOut="$(lsof -i :${port})"
    if [ -n "$portOut" ]; then
      out_warn Port: $port occupied. Please turn off port service.
    fi
  done
}

function check_is_enable_cgroup_memory() {
  mount | grep cgroup/memory | grep rw
  if [ $? -ne 0 ]; then
    out_warn "The machine not enabled cgroup memory. Please enable!"
  fi
}

function check_clean() {
  for cmd in docker kubelet kubectl; do
    which $cmd
    if [ $? -eq 0 ]; then
      out_warn The machine is not clean. Please reinstall the system.
    fi
  done
}

function check() {
  check_root
  check_os
  check_swap
  check_firewalld
  check_port
  check_is_enable_cgroup_memory
  check_clean
  out_success ">> Check Environment Finish! <<"
}

## check_is_install_success
function check_node_binary_service() {
  index=0
  for ((i = 0; i < 10; i++)); do
    dockerStatus="$(systemctl status docker | grep Active | awk '{print $3}')"
    kubeletStatus="$(systemctl status kubelet | grep Active | awk '{print $3}')"
    yurthubStatus="$(systemctl status yurthub | grep Active | awk '{print $3}')"
    if [[ $dockerStatus == "(running)" ]] &&
      [[ $yurthubStatus == "(running)" ]] &&
      [[ $kubeletStatus == "(running)" ]]; then
      break
    fi
    index=${i}
    sleep 1s
  done

  if [ "${index}" -le 10 ]; then
    return 0
  fi

  return 1
}

function check_is_install_success() {
  sleep 5s
  echo Waiting Running of the base service
  check_node_binary_service
  binIsRunning=$?
  if [ "$binIsRunning" -eq 0 ]; then
    echo Docker kubelet yurthub has Running!
  else
    out_warn Docker kubelet yurthub not all Running!
    return 1
  fi

  out_success Install Edge Node: $NODE_NAME Success!
  return 0
}

## install
function install_option() {
  echo install params: $*
  ARGS="$(getopt -o p:c:t:s: -a -l cloud-port:,cloud-host:,token:,site: -- "$@")"
  if [ $? != 0 ]; then
    echo "Terminating....." >&2
    exit 1
  fi

  eval set -- "$ARGS"
  while true; do
    case "$1" in
    -p | -cloud-port | --cloud-port)
      CLOUD_PORT=$2
      echo "Get option --cloud-port: $CLOUD_PORT"
      shift 2
      ;;
    -c | -cloud-host | --cloud-host)
      CLOUD_HOST=$2
      echo "Get option --cloud-host: $CLOUD_HOST"
      shift 2
      ;;
    -t | -token | --token)
      TOKEN=$2
      echo "Get option --token: $TOKEN"
      shift 2
      ;;
    -s | -site | --site)
      SITE=$2
      echo "Get option --site: $SITE"
      shift 2
      ;;
    "--")
      shift
      break
      ;;
    "?")
      echo "Unknown option $OPTARG"
      ;;
    *)
      echo "Unknown error while processing options"
      return 1
      ;;
    esac
  done
}

function note_message() {
  echo NOTE:
  echo "     input: [ edgectl $* ]"
  echo "     logPath: /tmp/edge-install.log"
  echo "     success-message: Install Edge Node: $NODE_NAME Success!"
  echo "     If the 'success-message does not appear, please check the log"
  echo
  echo Start Install Edge Node $NODE_NAME, Please Waiting...
}

function install() {
  clear &>/tmp/edge-install.log
  install_option $* &>>/tmp/edge-install.log
  init_tools
  init_env
  note_message $*
  install_script &>>/tmp/edge-install.log
  check_is_install_success
}

## init tools
function init_tools() {
  os="$(. /etc/os-release && echo "$ID")"
  if [ "$os" == "centos" ]; then
    yum install ipvsadm net-tools -y
  elif [ "$os" == "ubuntu" ]; then
    apt-get install ipvsadm net-tools -y
  fi
}

## init_env
function init_env() {
  if [ $UID -ne 0 ]; then
    echo "Non root user. Please run as root."
    exit -1
  else
    echo "Root user"
  fi

  if [ -z "$INTERFACE" ]; then
    INTERFACE=$(route | grep '^default' | grep -o '[^ ]*$')
  else
    echo ${INTERFACE} >/etc/interface
  fi

  IP_ADDR=$(ifconfig ${INTERFACE} | grep "inet " | awk '{print $2}')
  if [[ $IP_ADDR =~ 'addr' ]]; then
    IP_ADDR=${IP_ADDR##*:}
  fi
  echo "node ip is $IP_ADDR"

  if [ -z "$NODE_NAME" ]; then
    NODE_NAME=edgenode-${SITE}-${IP_ADDR}
  fi

  DNS_IP=169.254.20.11
  if [ -z "$DNS_IP" ]; then
    DNS_IP=$IP_ADDR
  fi

  EDGE_ARCH=
  if [ -z "$EDGE_ARCH" ]; then
    arch=$(uname -m)
    case $arch in
    x86_64)
      EDGE_ARCH="amd64"
      ;;
    *)
      echo "Unsupported arch $arch"
      exit -1
      ;;
    esac
  fi
  echo "arch is $EDGE_ARCH"
}

## install script
function install_script() {
  echo "Begin to prepare clean env..."
  mkdir -p /etc/kubernetes/pki
  mkdir -p /etc/sysconfig
  mkdir -p /root/.kube
  mkdir -p /var/lib/kubelet

  echo "Begin init system"
  modprobe br_netfilter
  modprobe ip_vs
  modprobe ip_vs_rr
  modprobe ip_vs_sh
  modprobe ip_vs_wrr
  modprobe ip_tables

  sysctl net.ipv4.ip_forward=1

  systemctl stop systemd-resolved
  systemctl disable systemd-resolved

  echo "begin to download && install docker"
  wget -q http://terminus-dice.oss.aliyuncs.com/installer/docker-19.03.6.tgz
  unalias cp
  tar -zvxf docker-19.03.6.tgz
  cp docker/* /usr/bin/

  cat >/etc/systemd/system/docker.service <<EOF
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target

[Service]
Type=notify
ExecStartPost=/usr/bin/env iptables -P FORWARD ACCEPT
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=infinity
LimitNPROC=infinity
TimeoutStartSec=0
Delegate=yes
KillMode=process
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target
EOF

  echo "Begin to download kubernetes..."
  ## 下载yurthub kubelet
  cd /usr/bin

  wget -q http://terminus-dice.oss.aliyuncs.com/installer/yurthub && chmod 755 yurthub
  wget -q http://terminus-dice.oss.aliyuncs.com/kubernetes/$KUBERNETES_VERSION/kubelet && chmod 755 kubelet
  wget -q http://terminus-dice.oss.aliyuncs.com/kubernetes/$KUBERNETES_VERSION/kubectl && chmod 755 kubectl

  if [ $? -eq 0 ]; then
    echo "====download kubernetes ok!===="
  else
    echo "====download kubernetes failed!===="
    exit -1
  fi

  echo "Close swap"
  swapoff -a || true
  sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab || true

  echo ${NODE_NAME} >/etc/node

  rm -rf /var/lib/cni/kube/

  cat >/etc/kubernetes/pki/ca.crt <<EOF
-----BEGIN CERTIFICATE-----
MIID6zCCAtOgAwIBAgIDJuHDMA0GCSqGSIb3DQEBCwUAMGoxKjAoBgNVBAoTIWM4
NGRmZTkyMzNkODY0OGZlYmEzOTY2YzRjZjVmYTBjMzEQMA4GA1UECxMHZGVmYXVs
dDEqMCgGA1UEAxMhYzg0ZGZlOTIzM2Q4NjQ4ZmViYTM5NjZjNGNmNWZhMGMzMB4X
DTIx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Y7i/iMjk1GDh2il+xECgYEAv+L4upQl7fa77lZPejJvvMui0cbEZK7Eaoi8
LhrhBzvFDBi8rH8WiTxKtMbwP1HFDtRJGdag+nZIRScwKC5txLUzWEQGZClTgcHF
86qqr7pWQId1/P98RtXcUnhNBMeIOA2TMhcnyAinx7hQ1TKxKBwj2UHfW5QG8YTH
jQvGoYkCgYAdICrqYWDxn4i1vOHyDeL5LaFiClcwGjgdLQ5Ar/szPBB15qVQXwNU
FAAch5PhHclqGp8lhIBMSz5OeombQhGs59Zctzd0qdoc2zo/mDh0ojkbEUTSqjS8
1IHu5twNFb2cBFfeMN2pSMcktg/yoQMyii8vkvr7g4ZOhZUWGk5+8w==
-----END RSA PRIVATE KEY-----
EOF

  cat >/etc/kubernetes/kubelet.conf <<EOF
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /etc/kubernetes/pki/ca.crt
    server: https://127.0.0.1:51003
  name: default-cluster
contexts:
- context:
    cluster: default-cluster
    user: kubelet-bootstrap
  name: default-system
current-context: default-system
kind: Config
preferences: {}
users:
- name: kubelet-bootstrap
  user:
    token: $TOKEN
EOF

  cat >/etc/systemd/system/yurthub.service <<'EOF'
[Unit]
Description=yurthub

[Service]
EnvironmentFile=-/etc/sysconfig/yurthub
ExecStart=/usr/bin/yurthub ${V} ${SERVER_ADDR} ${NODE_NAME} ${JOIN_TOKEN} ${SERVE_PORT} ${SERVER_TLS_CERT} ${SERVER_TLS_KEY}
Restart=always
RestartSec=10
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

  cat >/etc/sysconfig/yurthub <<EOF
V="--v=2"
SERVER_ADDR="--server-addr=https://${CLOUD_HOST}:${CLOUD_PORT}"
NODE_NAME="--node-name=${NODE_NAME}"
JOIN_TOKEN="--join-token=$TOKEN"
SERVE_PORT="--serve-port=51003"
SERVER_TLS_CERT="--server-tls-cert=/etc/kubernetes/pki/yurthub.pem"
SERVER_TLS_KEY="--server-tls-key=/etc/kubernetes/pki/yurthub-key.pem"
EOF

  cat >/etc/kubernetes/kubeconfig.conf <<EOF
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    server: https://127.0.0.1:51003
  name: default-cluster
contexts:
- context:
    cluster: default-cluster
    user: kubelet-bootstrap
  name: default-system
current-context: default-system
kind: Config
preferences: {}
users:
- name: kubelet-bootstrap
  user:
    tokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
EOF

  cat >/etc/systemd/system/kubelet.service <<EOF
[Unit]
Description=kubelet

[Service]
ExecStart=/usr/bin/kubelet \\
  --hostname-override=${NODE_NAME} \\
  --kubeconfig=/etc/kubernetes/kubelet.conf \\
  --config=/var/lib/kubelet/config.yaml \\
  --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/dice-third-party/pause:3.1 \\
  --network-plugin=cni \\
  --minimum-container-ttl-duration=1h \\
  --maximum-dead-containers-per-container=3 \\
  --maximum-dead-containers=60 \\
  --node-labels=dice/location-${SITE}=true,node.kubernetes.io/edge=true,openyurt.io/edge-enable-reverseTunnel-client=true,openyurt.io/is-edge-worker=true,topology.kubernetes.io/zone=${SITE}
Restart=always
RestartSec=10
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

  cat >/var/lib/kubelet/config.yaml <<'EOF'
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 2m0s
    enabled: true
  x509:
    clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 5m0s
    cacheUnauthorizedTTL: 30s
clusterDNS:
- 127.0.0.1
kubeAPIBurst: 10
kubeAPIQPS: 5
failSwapOn: false
maxPods: 110
clusterDomain: cluster.local
evictionHard:
  imagefs.available: 5%
  memory.available: 512Mi
  nodefs.available: 5%
  nodefs.inodesFree: 5%
EOF

  #cat > /etc/sysconfig/kubelet << EOF
  #HOSTNAME_OVERRIDE="--hostname-override=${NODE_NAME}"
  #KUBE_RESERVED="--kube-reserved=cpu=60m,memory=160Mi"
  #ANONYMOUS_AUTH="--anonymous-auth=false"
  #CLIENT_CA_FILE="--client-ca-file=/etc/kubernetes/pki/ca.crt"
  #AUTHORIZATION_MODE="--authorization-mode=Webhook"
  #CLOUD_CONFIG="--cloud-config="
  #NON_MASQUERADE_CIDR="--non-masquerade-cidr=0.0.0.0/0"
  #NETWORK_PLUGIN="--network-plugin=cni"
  #REGISTER_SCHEDULABLE="--register-schedulable=true"
  #IMAGE_PULL_PROGRESS_DEADLINE="--image-pull-progress-deadline=10m0s"
  #POD_INFRA_CONTAINER_IMAGE="--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/dice-third-party/pause:3.1"
  #CLUSTER_DNS="--cluster-dns=169.254.20.11"
  #CLUSTER_DOMAIN="--cluster-domain=cluster.local"
  #KUBECONFIG="--kubeconfig=/etc/kubernetes/kubelet.conf"
  #V="--v=2"
  #FAIL_SWAP_ON="--fail-swap-on=false"
  #MAX_PODS="--max-pods=61"
  #CLOUD_PROVIDER="--cloud-provider="
  #CNI_BIN_DIR="--cni-bin-dir=/opt/cni/bin/"
  #EVICTION_HARD="--eviction-hard=nodefs.available<10%,nodefs.inodesFree<5%,memory.available<100Mi"
  #AUTHENTICATION_TOKEN_WEBHOOK="--authentication-token-webhook=true"
  #REDIRECT="--redirect-container-streaming=false"
  #KUBE_API_BURST="--kube-api-burst=40"
  #KUBE_API_QPS="--kube-api-qps=20"
  #FEATUR_EGATES="--feature-gates=CSIMigration=false"
  #NODE_LABELS="--node-labels=dice/location-${SITE}=true,node.kubernetes.io/edge=true"
  #EOF

  #增加污点
  #REGISTER_WITH_TAINTS="--register-with-taints=zone=xxx"

  echo "complete init config"

  cd -

  mkdir -p /var/log/journal
  mkdir -p /var/lib/yurthub/pki
  ln -s /etc/kubernetes/pki/ca.crt /var/lib/yurthub/pki/ca.crt
  systemctl daemon-reload
  systemctl start yurthub
  systemctl start docker
  systemctl start kubelet
  systemctl enable docker
  systemctl enable yurthub
  systemctl enable kubelet

  echo "Health check!"
  status=$(systemctl status docker | grep Active | awk '{print $3}')
  if [[ $status == "(running)" ]]; then
    echo "docker is running"
  else
    echo "docker status is error"
    exit -1
  fi

  status=$(systemctl status yurthub | grep Active | awk '{print $3}')
  if [[ "${status}" == "(running)" ]]; then
    echo "yurthub is running"
  else
    echo "yurthub status is error"
    exit -2
  fi

  status=$(systemctl status kubelet | grep Active | awk '{print $3}')
  if [ "${status}" == "(running)" ]; then
    echo "kubelet is running"
  else
    echo "kubelet status is error"
    exit -3
  fi

}

## clear
function clear_container() {
  docker rm -f $(docker ps -aq)
}

function clear_bin() {
  for cmd in docker kubelet kubectl; do
    binPath="$(which $cmd)"
    rm -rf $binPath
  done
}

function clear() {
  ## stop service
  systemctl disable kubelet yurthub 2>/dev/null
  systemctl stop kubelet yurthub 2>/dev/null
  clear_container 2>/dev/null
  systemctl disable docker 2>/dev/null
  systemctl stop docker 2>/dev/null

  ## delete network
  ifconfig cni0 down >/dev/null 2>&1
  ifconfig flannel.1 down >/dev/null 2>&1
  ifconfig docker0 down >/dev/null 2>&1
  ip link delete cni0 >/dev/null 2>&1
  ip link delete flannel.1 >/dev/null 2>&1

  ## clear bin
  clear_bin >/dev/null 2>&1

  ## remove file
  rm -rfv /etc/kubernetes
  rm -rfv /etc/docker
  rm -fv /root/.kube/config
  rm -rfv /var/lib/kubelet >/dev/null 2>&1
  rm -rfv /var/lib/cni
  rm -rfv /etc/cni
  rm -rfv /var/lib/docker
  rm -rfv /etc/sysconfig/kubelet/
  rm -rfv /etc/sysconfig/yurthub
  rm -rfv /data/yurthub >/dev/null 2>&1
  rm -rfv /usr/lib/systemd/system/{kubelet,docker,yurthub}.service

  ## remove docker package
  out_success ">> Clear Node Complete! <<"
}

## help
function help_global() {
  echo '
Usage:
  edgectl command [flags]

Available Commands:
  check        Check the edge node if to be add to clusters
  install      Install components to edge node
  clear        Clear edge node and recovery as usual

Flags:
  -h, --help   Help for edgectl
'
}

function help_install() {
  echo '
Usage:
  edgectl install [flags]

Flags:
  -c,  --cloud-host    Edge cloud address, like 11.11.11.11, Must!
  -p,  --cloud-port    Edge cloud port, like 6443, Must!
  -t,  --token         Token use connect to edge cloud. Must!.
  -s   --site     r     Site name which the machine belong to, Must!.
'
}

function help() {
  case $1 in
  install)
    help_install
    return 0
    ;;
  check | clear | '' | *)
    help_global
    exit 0
    ;;
  esac
}

## main
function main() {
  case $1 in
  check)
    if [ -n "$2" ]; then
      help
      return 0
    fi
    check
    return 0
    ;;
  install)
    if [[ -n "$2" ]] && [[ "$2" == "-h" ]] || [[ -z "$3" ]]; then
      help install
      return 0
    fi
    install $*
    return 0
    ;;
  clear)
    if [ -n "$2" ]; then
      help
      return 0
    fi
    clear
    return 0
    ;;
  help | -h | --h | -help | --help | *)
    help $2
    exit 0
    ;;
  esac
}

main $*
